package com.pvdnc.xdynamic;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.pvdnc.dxhook.HookCallback;
import com.pvdnc.dxhook.ReplaceCallback;
import com.pvdnc.dxhook.XHookManager;
import com.pvdnc.xdynamic.common.ModuleStartArgs;
import com.pvdnc.xdynamic.loader.module.ModuleRecord;
import com.pvdnc.xdynamic.loader.module.XModuleManager;

import java.io.File;
import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {
    private static final String TAG=MainActivity.class.getSimpleName();

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        //SoUtils.getNativeLibPathList(getClassLoader());
        XModuleManager.systemReady(this);
        File apkFile = new File(getPackageCodePath());
        try {
            ModuleRecord record = XModuleManager.get().loadModule(apkFile);
            Context moduleContext=record.getModuleContext();
            record.callEntry(new ModuleStartArgs());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DaemonService.start(this);

       demoHookInstance();
    }

    private void demoHookInstance(){
        try {
            HookDemo hd=null;//new HookDemo();
            Method method = HookDemo.class.getMethod("getNewTitle", String.class);
            XHookManager.hookMethod(this,method,new ReplaceCallback(){
                @Override
                public void beforeHookedMethod(Method oldMethod ,Object thisRef, Object[] args) {
                    super.beforeHookedMethod(oldMethod, thisRef, args);
                    String title= (String) args[0];
                    Log.d(TAG,"before set title:"+title);
                    args[0]=title.replace("Null","2");
                }
            });
            hd=new HookDemo();
            String newTitle=hd.getNewTitle("Null");
            Log.d(TAG,"new instance title:"+newTitle);
            this.setTitle(newTitle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void demoHookStatic(){
        try {
            Method method = HookDemo.class.getMethod("getNewTitleStatic", String.class);
            XHookManager.hookMethod(this,method,new ReplaceCallback(){
                @Override
                public void beforeHookedMethod(Method oldMethod, Object thisRef, Object[] args) {
                    super.beforeHookedMethod(oldMethod, thisRef, args);
                    String title= (String) args[0];
                    Log.d(TAG,"before set title:"+title);
                    args[0]=title.replace("Null","2");
                }
            });
            String newTitle=HookDemo.getNewTitleStatic("Null");
            Log.d(TAG,"new static title:"+newTitle);
            this.setTitle(newTitle);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
